﻿
<%

Class BonusAPI

'***************************************************
'函数名：wonAPI
'作  用：抽奖
'参  数：intPercent ---概率比
'参  数：strPids ----奖品编号
'参  数：strProbaly ----奖品概率
'返回值：num  ----中奖礼品编号
'***************************************************
Function wonAPI(intPercent,strPids,strProbaly)
	
	Dim arrPids,arrProbaly,i
	
	arrPids = Split(strPids,"|")
	
	arrProbaly = Split(strProbaly,"|")
		
	wonAPI = checkBonus(intPercent, arrPids, arrProbaly)
	
End Function

'***************************************************
'函数名：getSum
'作  用：数组指定索引求和
'参  数：intIndex ---索引
'参  数：arrProbaly ----奖品概率数组
'返回值：num  ---指定索引之前的和
'***************************************************
Function getSum(intIndex,arrProbaly)
	
	Dim intSum
	
	intSum= 0
	
	Dim i
		
	For i=0 to intIndex 
		
		intSum = intSum + cint(arrProbaly(i)) 
		
	Next
		
	getSum = intSum
	
End Function

'***************************************************
'函数名：wonAPI
'作  用：抽奖
'参  数：intPercent ---概率比
'参  数：arrPids ----奖品编号数组
'参  数：arrProbaly ----奖品概率数组
'返回值：num  ----中奖礼品编号
'***************************************************
Function checkBonus(intPercent, arrPids, arrProbaly)
	
	Dim resultPid 
		
	Dim low,curRnd,high
	
	low = 0
		
	high = ubound(arrPids)
			
	Randomize 
	
	curRnd  =  int(intPercent*Rnd) 	

	Do while (low <= high)
						
		Dim	middleid,tempSum
		
		middleid = (low + high) / 2
				
		middleid = cint(middleid)
		
		tempSum = getSum(middleid,arrProbaly)
				
		If  curRnd = tempSum  Then

				for i=(middleid+1) to high
				
					if  arrProbaly(i) <> 0 then
		
						resultPid = arrPids(i)
					
						exit for
					
					end if
				
				next
		
				exit do

			ElseIf curRnd < tempSum Then

				If  middleid > 0 Then
											
					If curRnd >= getSum(middleid- 1, arrProbaly) Then
						
						resultPid = arrPids(middleid)
		
						Exit do
						
					Else
						
						high = middleid - 1
							
					End If
						
				Else
					
					resultPid = arrPids(middleid)
		
					Exit do
				
				End If
					
			Else
				
					low = middleid + 1

		End If
		
	Loop
			
	checkBonus  = resultPid
	
End Function

End Class

%>
